<div style="padding:5px">
	<ul class="ztree" id="${id}" ></ul>
</div>
<script type="text/javascript">
			
			var setting = {
				async: {
					enable : true,
					url : '${cxt!}/organization/treeData',
					autoParam : ["id=ids"],//, "name=n", "level=lv"
					//otherParam : {"otherParam" : "zTreeAsyncTest"},
					type : "post"
				},
				view: {
					fontCss: getFont,
					expandSpeed: "",
					addHoverDom: addHoverDom,
					removeHoverDom: removeHoverDom,
					selectedMulti: false
				},
				edit: {
					enable: true,
					removeTitle: "删除",
					renameTitle: "编辑"
				},
				data: {
					simpleData: {
						enable: true
					}
				},
				callback: {
					onClick: zTreeOnClick,
					beforeRemove: beforeRemove, //节点被删除之前的事件,并且根据返回值确定是否允许删除操作
					beforeRename: beforeRename, //用于捕获节点编辑名称结束

					beforeAsync: beforeAsync, //用于捕获异步加载之前的事件回调函数,zTree 根据返回值确定是否允许进行异步加载
					onAsyncSuccess: onAsyncSuccess, //用于捕获异步加载出现异常错误的事件回调函数
					onAsyncError: onAsyncError, //用于捕获异步加载正常结束的事件回调函数
				}
			};
			var data = [ /**自定义的数据源，ztree支持json,数组，xml等格式的**/
				{ id: 0, pId: -1, name: "瘦吧总部", icon: "../../plugins/eovaicon/icon/world.png" },
				{ id: 1, pId: 0, name: "北京(1)", icon: "../../plugins/eovaicon/icon/world.png"  },
				{ id: 11, pId: 1, name: "海淀区代理", icon: "../../plugins/eovaicon/icon/world.png"  },
//				{ id: 2, pId: 0, name: "天津", icon: "../../plugins/eovaicon/icon/world.png"  },
				{ id: 3, pId: 0, name: "上海(1)", icon: "../../plugins/eovaicon/icon/world.png"  },
				{ id: 31, pId: 3, name: "浦东代理", icon: "../../plugins/eovaicon/icon/world.png"  },
				{ id: 6, pId: 0, name: "山西省(2)", icon: "../../plugins/eovaicon/icon/world.png"  },
				{ id: 61, pId: 6, name: "太原代理(2)", icon: "../../plugins/eovaicon/icon/world.png"  },
//				{ id: 611, pId: 61, name: "西一巷门店", icon: "../../plugins/eovaicon/icon/world.png"  },
//				{ id: 612, pId: 61, name: "云水榭门店", icon: "../../plugins/eovaicon/icon/world.png" },
				{ id: 62, pId: 6, name: "长治代理", icon: "../../plugins/eovaicon/icon/world.png"  }
			];
			
			function zTreeOnClick(event, treeId, treeNode) {
				var data;
				var columns;
				var title;
//				$('#table_div').html('<table id="deptTable" style="height:100%"></table>');
				if(treeNode.id == 0){
					
					data = [ /**自定义的数据源，ztree支持json,数组，xml等格式的**/
						{deptName: "海淀区代理", manager: "小赵", mPhone: "010-1234567", principal: "王总", pPhone: "0351-7654321", createTime: "2017-04-05"},
						{deptName: "浦东代理", manager: "小王", mPhone: "021-1234567", principal: "王总", pPhone: "0351-7654321", createTime: "2017-04-05"},
						{deptName: "太原代理", manager: "小刘", mPhone: "0351-1234567", principal: "刘总", pPhone: "123456789", createTime: "2017-04-05"},
						{deptName: "长治代理", manager: "小张", mPhone: "0355-1234567", principal: "刘总", pPhone: "123456789", createTime: "2017-04-05"}
					];
					
					columns = [[
//						{field:'department',title:'所属机构',width:80},
						{field:'deptName',title:'名称',width:100,align:'center'},
						{field:'manager',title:'代理人',width:80,align:'center'},
						{field:'mPhone',title:'电话',width:160,align:'center'},
						{field:'principal',title:'市场经理',width:80,align:'center'},
						{field:'pPhone',title:'电话',width:120,align:'center'},
						{field:'createTime',title:'代理日期',width:120,align:'center'},
						{field:'operate', width:120, align:'center', formatter:button}
				    ]];
				    title = "总部";
					
				}
				if(treeNode.id == 1){
					
					data = [ /**自定义的数据源，ztree支持json,数组，xml等格式的**/
						{deptName: "海淀区代理", manager: "小赵", mPhone: "010-1234567", principal: "王总", pPhone: "0351-7654321", createTime: "2017-04-05"}
					];
					
					columns = [[
//						{field:'department',title:'所属机构',width:80},
						{field:'deptName',title:'名称',width:100,align:'center'},
						{field:'manager',title:'代理人',width:80,align:'center'},
						{field:'mPhone',title:'电话',width:160,align:'center'},
						{field:'principal',title:'市场经理',width:80,align:'center'},
						{field:'pPhone',title:'电话',width:120,align:'center'},
						{field:'createTime',title:'代理日期',width:120,align:'center'},
						{field:'operate', width:120, align:'center', formatter:button}
				    ]];
				    title = "北京";
					
				}
				if(treeNode.id == 3){
					
					data = [ /**自定义的数据源，ztree支持json,数组，xml等格式的**/
						{deptName: "浦东代理", manager: "小王", mPhone: "021-1234567", principal: "王总", pPhone: "0351-7654321", createTime: "2017-04-05"}
					];
					
					columns = [[
//						{field:'department',title:'所属机构',width:80},
						{field:'deptName',title:'名称',width:100,align:'center'},
						{field:'manager',title:'代理人',width:80,align:'center'},
						{field:'mPhone',title:'电话',width:160,align:'center'},
						{field:'principal',title:'市场经理',width:80,align:'center'},
						{field:'pPhone',title:'电话',width:120,align:'center'},
						{field:'createTime',title:'代理日期',width:120,align:'center'},
						{field:'operate', width:120, align:'center', formatter:button}
				    ]];
				    title = "上海";
					
				}
				if(treeNode.id == 6){
					data = [ /**自定义的数据源，ztree支持json,数组，xml等格式的**/
						{deptName: "太原代理", manager: "小刘", mPhone: "0351-1234567", principal: "刘总", pPhone: "123456789", createTime: "2017-04-05"},
						{deptName: "长治代理", manager: "小张", mPhone: "0355-1234567", principal: "刘总", pPhone: "123456789", createTime: "2017-04-05"}
					];
					columns = [[
//						{field:'department',title:'所属机构',width:80},
						{field:'deptName',title:'名称',width:100,align:'center'},
						{field:'manager',title:'代理人',width:80,align:'center'},
						{field:'mPhone',title:'电话',width:160,align:'center'},
						{field:'principal',title:'市场经理',width:80,align:'center'},
						{field:'pPhone',title:'电话',width:120,align:'center'},
						{field:'createTime',title:'代理日期',width:120,align:'center'},
						{field:'operate', width:120, align:'center', formatter:button}
				    ]];
				    title = "山西市场";
				}
				if(treeNode.id == 61){
					data = [ /**自定义的数据源，ztree支持json,数组，xml等格式的**/
						{deptName: "云水榭门店", manager: "小赵", mPhone: "0351-1234567", principal: "刘总", pPhone: "123456789", createTime: "2017-04-05"},
						{deptName: "西一巷门店", manager: "小王", mPhone: "0351-7654321", principal: "刘总", pPhone: "123456789", createTime: "2017-04-05"}
					];
					columns = [[
//						{field:'department',title:'所属机构',width:80},
						{field:'deptName',title:'名称',width:100,align:'center'},
						{field:'manager',title:'店长',width:80,align:'center'},
						{field:'mPhone',title:'电话',width:160,align:'center'},
						{field:'principal',title:'片区经理',width:80,align:'center'},
						{field:'pPhone',title:'电话',width:120,align:'center'},
						{field:'createTime',title:'代理日期',width:120,align:'center'},
						{field:'operate', width:120, align:'center', formatter:button}
				    ]];
				    title = "太原市场";
				}
				$('#deptTable').datagrid({
								cls: "theme-datagrid",
								title: title,
								singleSelect: true,
								rownumbers: true, //显示序号
								pagination: true,
								pageSize: 10,
								collapsible: true,
								columns: columns,
								data: data,
								onClickRow:function(rowIndex,rowData){
									var data;
									var title;
//									$('#userTable').datagrid('loadData', '');
					        		if(rowData.deptName == '海淀区代理'){
										data = [ /**自定义的数据源，ztree支持json,数组，xml等格式的**/
											{name: "小赵", username: "admin", station: "代理商", phone: "0351-7654321"},
											{name: "王五", username: "wangwu", station: "片区经理", phone: "0351-7654321"}
										];
										title = '海淀区代理';
									}
									if(rowData.deptName == '太原代理'){
										data = [ /**自定义的数据源，ztree支持json,数组，xml等格式的**/
											{name: "小刘", username: "ty_admin", station: "代理商", phone: "0351-7654321"},
											{name: "王五", username: "wangwu", station: "片区经理", phone: "0351-7654321"}
										];
										title = '太原代理';
									}
									if(rowData.deptName == '云水榭门店'){
										data = [ /**自定义的数据源，ztree支持json,数组，xml等格式的**/
											{name: "赵六", username: "admin", station: "店长", phone: "0351-7654321"},
											{name: "钱七", username: "wangwu", station: "店员", phone: "0351-7654321"}
										];
										title = '云水榭门店';
									}
									if(rowData.deptName == '西一巷门店'){
										data = [ /**自定义的数据源，ztree支持json,数组，xml等格式的**/
											{name: "孙八", username: "admin", station: "店长", phone: "0351-7654321"},
											{name: "周九", username: "wangwu", station: "店员", phone: "0351-7654321"}
										];
										title = '西一巷门店';
									}
									if(rowData.deptName == '长治代理'){
										data = [ /**自定义的数据源，ztree支持json,数组，xml等格式的**/
											{name: "李四", username: "admin", station: "代理商", phone: "0351-7654321"}
										];
										title = '长治代理';
									}
									$("#userTable").datagrid({title:title + "用户列表"});
									$('#userTable').datagrid("loadData", data);
								}
					});
//				$('#deptTable').datagrid("loadData", data);
			    
			};
	
			//字体设置
			function getFont(treeId, node) {
				return node.font ? node.font : {};
			}

			////////////////////下面是处理增删改节点//////////////////

			//节点被删除之前的事件,并且根据返回值确定是否允许删除操作
			function beforeRemove(treeId, treeNode) {
				var zTree = $.fn.zTree.getZTreeObj("organization_tree");
				zTree.selectNode(treeNode);

				var result = false;
				if(confirm("确认删除<" + treeNode.name + ">机构吗？")) {
//					var url = "/platform/dept/delete";
//					var data = { "ids": treeNode.id};

//					var reData = common_ajax.ajaxFunc(url, data);
//					if(reData == "false") {
//						common_modals.alert("删除<" + treeNode.name + ">部门失败！");
//					} else {
//						result = true;
//					}
					result = true;
				}

				return result;
			}

			//用于捕获节点编辑名称
			function beforeRename(treeId, treeNode, newName, isCancel) {
				if(treeNode.name == newName) {
					return true;
				} else if(newName.length == 0) {
					common_modals.alert("机构名称不能为空！");
					return false;
				} else {
//					var url = "/platform/dept/update";
//					var data = { "ids": treeNode.id, "names": newName, "csrfToken": "${csrfToken!}" };
//
//					var reData = common_ajax.ajaxFunc(url, data);
//					if(reData == treeNode.id) {
						return true;
//					} else {
//						common_modals.alert("修改<" + treeNode.name + ">部门名称失败！");
//						return false;
//					}
				}
			}

			//添加功能按钮
			var newCount = 1;

			function addHoverDom(treeId, treeNode) {
				var sObj = $("#" + treeNode.tId + "_span");
				if(treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) {
					return;
				}

				//1.处理添加按钮
//				var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='添加部门' onfocus='this.blur();'></span>";
//				sObj.after(addStr);
//				var btn = $("#addBtn_" + treeNode.tId);
//				if(btn) btn.bind("click", function() {
//					var newId = (newCount++);
//					var addName = "新增部门" + newId; //部门初始化名称
////					var orderIds = (treeNode.children == undefined ? 1 : (treeNode.children.length + 1));
//
////					var url = "/platform/dept/save";
////					var data = {
////						"pIds": treeNode.id,
////						"names": addName,
////						"orderIds": orderIds,
////						"csrfToken": "${csrfToken!}"
////					};
//
////					var reData = common_ajax.ajaxFunc(url, data);
////					if(reData != "") {
//						var zTree = $.fn.zTree.getZTreeObj("organization_tree");
//						zTree.addNodes(treeNode, { id: newId, pId: treeNode.tId, name: addName });
//						var node = zTree.getNodeByParam("id", newId, null); //根据新的id找到新添加的节点  
//         		 		zTree.selectNode(node); //让新添加的节点处于选中状态  
////					} else {
////						common_modals.alert("新增部门失败！");
////					}
//
////					return false;
//				});

				//2.部门负责人设置按钮
//				var operatorStr = "<span class='button edit' id='operatorBtn_" + treeNode.tId + "' title='设置部门负责人' onfocus='this.blur();'></span>";
				var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='添加' onfocus='this.blur();'></span>";
				sObj.after(addStr);
				var btn = $("#addBtn_" + treeNode.tId);
				if(btn) btn.bind("click", function() {
// 					$("#dlg").dialog("open");
					loadDialog('添加代理', '/organization/toAdd');
				});
				//3.设置权限
				var operatorStr = "<span class='button edit' id='operatorBtn_" + treeNode.tId + "' title='设置权限' onfocus='this.blur();'></span>";
				sObj.after(operatorStr);
				var btn = $("#operatorBtn_" + treeNode.tId);
				if(btn) btn.bind("click", function() {
					$("#station_dlg").dialog("open");
				});
			};

			function removeHoverDom(treeId, treeNode) {
				//1.处理添加按钮
				$("#addBtn_" + treeNode.tId).unbind().remove();
				//2.处理查看按钮
				$("#operatorBtn_" + treeNode.tId).unbind().remove();
			};

			////////////////////下面是处理展开//////////////////

			//用于捕获异步加载之前的事件回调函数,zTree 根据返回值确定是否允许进行异步加载
			function beforeAsync() {
				curAsyncCount++;
			}

			//用于捕获异步加载出现异常错误的事件回调函数
			function onAsyncSuccess(event, treeId, treeNode, msg) {
				curAsyncCount--;
				if(curStatus == "expand") {
					expandNodes(treeNode.children);
				} else if(curStatus == "async") {
					asyncNodes(treeNode.children);
				}

				if(curAsyncCount <= 0) {
					if(curStatus != "init" && curStatus != "") {
						//$("#demoMsg").text((curStatus == "expand") ? "全部展开完毕" : "后台异步加载完毕");
						asyncForAll = true;
					}
					curStatus = "";
				}
			}

			//用于捕获异步加载正常结束的事件回调函数
			function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
				curAsyncCount--;

				if(curAsyncCount <= 0) {
					curStatus = "";
					if(treeNode != null) asyncForAll = true;
				}
			}

			var curStatus = "init",
				curAsyncCount = 0,
				asyncForAll = false,
				goAsync = false;

			function expandAll() {
				if(!check()) {
					return;
				}
				var zTree = $.fn.zTree.getZTreeObj("organization_tree");
				if(asyncForAll) {
					//$("#demoMsg").text("已经异步加载完毕，使用 expandAll 方法");
					zTree.expandAll(true);
				} else {
					expandNodes(zTree.getNodes());
					if(!goAsync) {
						//$("#demoMsg").text("已经异步加载完毕，使用 expandAll 方法");
						curStatus = "";
					}
				}
			}

			function expandNodes(nodes) {
				if(!nodes) {
					return;
				}
				curStatus = "expand";
				var zTree = $.fn.zTree.getZTreeObj("organization_tree");
				for(var i = 0, l = nodes.length; i < l; i++) {
					zTree.expandNode(nodes[i], true, false, false);
					if(nodes[i].isParent && nodes[i].zAsync) {
						expandNodes(nodes[i].children);
					} else {
						goAsync = true;
					}
				}
			}

			function asyncAll() {
				if(!check()) {
					return;
				}
				var zTree = $.fn.zTree.getZTreeObj("organization_tree");
				if(asyncForAll) {
					//$("#demoMsg").text("已经异步加载完毕，不再重新加载");
				} else {
					asyncNodes(zTree.getNodes());
					if(!goAsync) {
						//$("#demoMsg").text("已经异步加载完毕，不再重新加载");
						curStatus = "";
					}
				}
			}

			function asyncNodes(nodes) {
				if(!nodes) {
					return;
				}
				curStatus = "async";
				var zTree = $.fn.zTree.getZTreeObj("organization_tree");
				for(var i = 0, l = nodes.length; i < l; i++) {
					if(nodes[i].isParent && nodes[i].zAsync) {
						asyncNodes(nodes[i].children);
					} else {
						goAsync = true;
						zTree.reAsyncChildNodes(nodes[i], "refresh", true);
					}
				}
			}

			function reset() {
				if(!check()) {
					return;
				}
				asyncForAll = false;
				goAsync = false;
				//$("#demoMsg").text("");
				$.fn.zTree.init($("#treeDemo"), setting);
			}

			function check() {
				if(curAsyncCount > 0) {
					//$("#demoMsg").text("正在进行异步加载，请等一会儿再点击...");
					return false;
				}
				return true;
			}



			//////////////////初始化////////////////////

			$(document).ready(function() {
				$.fn.zTree.init($("#organization_tree"), setting);
			});

			setTimeout(function() {
				expandAll();
			}, 500);
		</script>